package com.ego.passport.controller;

import com.ego.commons.pojo.EgoResult;
import com.ego.passport.service.TbUserService;
import com.ego.pojo.TbUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@Controller
public class PassportController {

    @Autowired
    private TbUserService userService;

    @RequestMapping("/user/showLogin")
    public String userShowLogin(@RequestHeader(name = "Referer") String referer, Model model, HttpServletRequest  httpRequest){

        // model.addAttribute("redirect",referer);


        return "login";
    }

//    @RequestMapping("/user/showLogin")
//    public String showLogin(@RequestHeader(name = "Referer",required = false) String referer, Model model){
//        if(Strings.isNotEmpty(referer)){
//            if(!referer.endsWith("/user/showRegister")){
//                model.addAttribute("redirect",referer);
//            }
//        }
//        return "login";
//    }


        @RequestMapping("/user/showRegister")
    public String showRegister() {
        return "register";
    }
    @GetMapping("/user/check/{param}/{type}")
    @ResponseBody
    public EgoResult userCheck(@PathVariable String param,@PathVariable int type){
      return  userService.checkUser(param,type);
    }

    @RequestMapping("/user/register")
    @ResponseBody
    public EgoResult userRegister(TbUser user){
        String username = user.getUsername();
        String phone = user.getPhone();
        String email = user.getEmail();
        if (StringUtils.isBlank(username)||StringUtils.isNotBlank(username)&&!username.matches("^[a-zA-Z]\\w{5,15}$")){
            return EgoResult.error("账号必须首字母开头，长度6-16");
        }else if(StringUtils.isBlank(email) || StringUtils.isNotBlank(email) && !email.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")){
            return EgoResult.error("请输入正确邮箱");
        }else if(StringUtils.isBlank(phone) || StringUtils.isNotBlank(phone) && !phone.matches("^1[3|4|5|6|7|8|9]\\d{9}$")){
            return EgoResult.error("请输入正确的手机号");
        }
        return userService.register(user);
    }
    @RequestMapping("/user/login")
    @ResponseBody
    public EgoResult login(TbUser tbUser, HttpSession httpSession){
        TbUser user=userService.login(tbUser);

        //将登陆的用户共享到Session中
        if (user!=null){
            httpSession.setAttribute("loginUser",user);
            return EgoResult.ok();
        }
        return EgoResult.error("账号密码错误，请重新登录");
    }
@RequestMapping("/user/token/{token}")
@ResponseBody
//允许接收跨域请求的Cookie，默认是不允许接收跨域请求的Cookie
@CrossOrigin(allowCredentials = "true")
    public EgoResult userToken(HttpSession session){
    TbUser loginUser = (TbUser) session.getAttribute("loginUser");
    if (loginUser!=null){
        return EgoResult.ok(loginUser);
    }
    return EgoResult.error("没有登录..");
}
@RequestMapping("/user/logout/{token}")
@ResponseBody
@CrossOrigin(allowCredentials = "true")
public EgoResult logout(HttpSession session){
        session.removeAttribute("loginUser");
    //清除Session中数据
    session.invalidate();

        return EgoResult.ok("退出登录成功");
}


}
